﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.Image;
using CE.User;
using CE.Global;
using CE.Company;

public partial class contactus_ContactUs: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["ImageCount"] != null)
        {
            if (Convert.ToInt32(Session["ImageCount"].ToString()) > 5)
            {
                lblMessage.Text = "Please try after a few moment! You are session has been locked!";
                divUNSError.Visible = true;
                btnUnSubscribe.Enabled = false;
                btnNewImage.Enabled = false;
                txtEmail.Enabled = false;
                txtSecurityCode.Enabled = false;
                Response.Redirect("usermessage.aspx?mt=um&ec=1993");
            }
            else
            {
                lblMessage.Text = "";
                divUNSError.Visible = false;
                btnUnSubscribe.Enabled = true;
                btnNewImage.Enabled = true;
                txtEmail.Enabled = true;
                txtSecurityCode.Enabled = true;
            }
        }
        if (!Page.IsPostBack)
        {
            LogUserViewedHistory();
            ClearUnSubscribe(false);
            lblExist.Text = "";
            lblThankYou.Text = "";
            lblUnSubscribeMessage.Text = "";
            CompanyInformation company = new CompanyInformation();
            CompanyInformationDB _companydb = new CompanyInformationDB();
            company = _companydb.GetCompanyInformation(2);
            if (company != null)
            {
                lblContactAddress.Text = company.Address1;
                hplSendUsEmail.NavigateUrl = "mailto:" + company.EmailAddress;
                hplSendUsEmail.Text = company.EmailAddress;
            }
            txtEmail.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmail.ClientID + "','" + rfvEmail.ClientID + "','" + revEmail.ClientID + "');");
            txtUnSubscribeEmail.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtUnSubscribeEmail.ClientID + "','" + rfvUnSubscribeEmail.ClientID + "','" + revUnSubscribeEmail.ClientID + "');");
            txtSecurityCode.Attributes.Add("onblur", "ShowValidationEffect('" + txtSecurityCode.ClientID + "','" + rfvSecurityCheck.ClientID + "');");

            if (Session["ImageCount"] != null)
            {
                if (Convert.ToInt32(Session["ImageCount"].ToString()) <= 5)
                {
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                CreateUpdateImageAndSession();
            }
            //FeedBack
            txtName.Text = "";
            txtFBEmail.Text = "";
            txtFeedBack.Text = "";
            if (Request.QueryString["sc"] != null && Request.QueryString["sc"].ToString() == "1")
            {
                divFBThankYou.Visible = true;
                divFeedback.Visible = false;
                divButton.Visible = false;
            }
            else
            {
                divFeedback.Visible = true;
                divButton.Visible = true;
                divFBThankYou.Visible = false;
            }
        }
        ChangeProviderLogoList();
    }

    private void ChangeProviderLogoList()
    {
        //    100	Car Insurance
        //    200	Home Refinance
        //    300	Credit Card
        //    400	Personal Loan
        //    500	Personal Accident
        //    600	Travel Insurance

        //Random rnd = new Random();
        //int pValue = rnd.Next(1, 6);
        //pValue = pValue * 100;
        
        //usrCompanyLogoByServices1.CompanyProviderTypeId = pValue.ToString();
        //rnd = null;
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("CTUS", Request, "Contact Us.");
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        //Loan Purpose
        if (rfvEmail.Enabled && rfvEmail.ValidationGroup == strValidationGroup)
        {
            rfvEmail.Validate();
            if (rfvEmail.IsValid == false)
            {
                txtEmail.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtEmail.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtEmail.ControlStyle.CssClass = "";
        }

        //UnSubscribeEmail
        if (rfvUnSubscribeEmail.Enabled && rfvUnSubscribeEmail.ValidationGroup == strValidationGroup)
        {
            rfvUnSubscribeEmail.Validate();
            if (rfvUnSubscribeEmail.IsValid == false)
            {
                txtUnSubscribeEmail.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtUnSubscribeEmail.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtUnSubscribeEmail.ControlStyle.CssClass = "";
        }

        if (revUnSubscribeEmail.Enabled && revUnSubscribeEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvUnSubscribeEmail.IsValid == true)
            {
                revUnSubscribeEmail.Validate();
                if (revUnSubscribeEmail.IsValid == false)
                {
                    txtUnSubscribeEmail.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtUnSubscribeEmail.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtUnSubscribeEmail.ControlStyle.CssClass = "";
        }

        //Security Code
        if (rfvSecurityCheck.Enabled && rfvSecurityCheck.ValidationGroup == strValidationGroup)
        {
            rfvSecurityCheck.Validate();
            if (rfvSecurityCheck.IsValid == false)
            {
                txtSecurityCode.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtSecurityCode.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtSecurityCode.ControlStyle.CssClass = "";
        }

        //Feed Back
        //Feed Back Name
        if (rfvName.Enabled && rfvName.ValidationGroup == strValidationGroup)
        {
            rfvName.Validate();
            if (rfvName.IsValid == false)
            {
                txtName.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtName.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtName.ControlStyle.CssClass = "";
        }

        if (revName.Enabled && revName.ValidationGroup == strValidationGroup)
        {
            if (rfvName.IsValid == true)
            {
                revName.Validate();
                if (revName.IsValid == false)
                {
                    txtName.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtName.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtName.ControlStyle.CssClass = "";
        }

        //Feed Back Email
        if (rfvFBEmail.Enabled && rfvFBEmail.ValidationGroup == strValidationGroup)
        {
            rfvFBEmail.Validate();
            if (rfvFBEmail.IsValid == false)
            {
                txtFBEmail.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtFBEmail.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtFBEmail.ControlStyle.CssClass = "";
        }

        if (revFBEmail.Enabled && revFBEmail.ValidationGroup == strValidationGroup)
        {
            if (rfvFBEmail.IsValid == true)
            {
                revFBEmail.Validate();
                if (revFBEmail.IsValid == false)
                {
                    txtFBEmail.ControlStyle.CssClass = "validationError";
                }
                else
                {
                    txtFBEmail.ControlStyle.CssClass = "";
                }
            }
        }
        else
        {
            txtFBEmail.ControlStyle.CssClass = "";
        }

        //Remark
        if (rfvFeedback.Enabled && rfvFeedback.ValidationGroup == strValidationGroup)
        {
            rfvFeedback.Validate();
            if (rfvFeedback.IsValid == false)
            {
                txtFeedBack.ControlStyle.CssClass = "validationError";
            }
            else
            {
                txtFeedBack.ControlStyle.CssClass = "";
            }
        }
        else
        {
            txtFeedBack.ControlStyle.CssClass = "";
        }

    }

    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        lblExist.Text = "";
        lblThankYou.Text = "";
        ClearUnSubscribe(false);
        Page.Validate("subscriber");
        ChangeValidationEffectServerSide("subscriber");
        if (Page.IsValid)
        {
            Int64 _Subscribe = -1;
            UserSubscriber _usersub = new UserSubscriber();
            UserSubscriberDB _usersubdb = new UserSubscriberDB();
            _usersub.Email = txtEmail.Text;
            _Subscribe = _usersubdb.SubscribeUser(_usersub);
            if (_Subscribe > 0)
            {
                divThankYou.Visible = true;
                lblExist.Text = "";
                lblThankYou.Text = "Your email has been added in E-Newsletter list. Thank you very much!";
                txtEmail.Enabled = false;
                btnSubscribe.Enabled = false;
            }
            else if (_Subscribe == 0)
            {
                divThankYou.Visible = true;
                lblThankYou.Text = "";
                lblExist.Text = "Your email is already existing in our list!";
            }
            else if (_Subscribe < 0)
            {
                divThankYou.Visible = true;
                lblThankYou.Text = "";
                lblExist.Text = "Subscribed failed!Try again later!";
            }
            /*//Sakthi on 12 Jun 2012 -- Start 
            if (_usersubdb.AlreadyExist(_usersub.Email) > 0)
            {
                divThankYou.Visible = true;
                lblThankYou.Text = "";
                lblExist.Text = "Your email is already existing in our list!";
            }
            else if (_usersubdb.Insert(_usersub) > 0)
            {
                divThankYou.Visible = true;
                lblExist.Text = "";
                lblThankYou.Text = "Your email has been added in E-Newsletter list. Thank you very much!";
                txtEmail.Enabled = false;
                btnSubscribe.Enabled = false;
            }  //Sakthi on 12 Jun 2012 -- End */
        }
    }

    protected void lbtnToUnSubscribe_Click(object sender, EventArgs e)
    {
        ClearUnSubscribe(true);
    }

    protected void btnNewImage_Click(object sender, EventArgs e)
    {
        CreateUpdateImageAndSession();
    }

    private void CreateUpdateImageAndSession()
    {
        MyImage myimage = new MyImage();
        string salt = "";
        if (Session[CE.Global.General.SecurityCheckWord] != null)
        {
            salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
        }
        else
        {
            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
            }
            else
            {
                salt = myimage.GetRandomWord("all");
            }
        }

        Session[CE.Global.General.SecurityCheckWord] = salt;
        imgCAPTCHA.ImageUrl = @"~/CAPTCHA/CAPTCHAImageHandler.ashx?id=" + CE.Security.MySecurity.Encrypt(salt, true);
        txtSecurityCode.Text = "";
    }

    public string CreateSalt(int size)
    {
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        Byte[] buff = new Byte[size];
        rng.GetBytes(buff);
        return Convert.ToBase64String(buff);
    }

    protected void btnUnSubscribe_Click(object sender, EventArgs e)
    {
        btnSubscribe.Enabled = true;
        txtEmail.Enabled = true;
        txtEmail.Text = "";
        lblThankYou.Text = "";
        lblExist.Text = "";
        divThankYou.Visible = false;
        Page.Validate("unsubscribe");
        ChangeValidationEffectServerSide("unsubscribe");
        if (Page.IsValid)
        {
            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                string _usrText = txtSecurityCode.Text.ToUpper();
                string _imgText = Session[CE.Global.General.SecurityCheckWord].ToString().ToUpper();
                if (_usrText == _imgText)
                {
                    Int64 _Subscribe = -1;
                    UserSubscriber _usersub = new UserSubscriber();
                    UserSubscriberDB _usersubdb = new UserSubscriberDB();
                    _Subscribe = _usersubdb.UnSubscribeUser(txtUnSubscribeEmail.Text.Trim());
                    if (_Subscribe > 0)
                    {
                        lblUnSubscribeMessage.Text = "Your email is unsubscribed from our list!";
                        lblMessage.Text = "";
                        divUNSError.Visible = false;
                    }
                    else if (_Subscribe == 0)
                    {
                        lblUnSubscribeMessage.Text = "Your email is already unsubscribed in our list!";
                        lblMessage.Text = "";
                        divUNSError.Visible = false;
                    }
                    else
                    {
                        lblUnSubscribeMessage.Text = "";
                        lblMessage.Text = "Unsubscribed failed!Try again later!";
                        divUNSError.Visible = true;
                    }

                    /* // Sakthi on 12 Jun 2012 -- Start
                    _usersub.Email = txtUnSubscribeEmail.Text;
                    if (_usersubdb.AlreadyExist(_usersub.Email) > 0)
                    {
                        if (_usersubdb.AlreadyExist(_usersub.Email, true) > 0)
                        {
                            _usersub.SubscriberID = _usersubdb.AlreadyExist(_usersub.Email);
                            if (_usersubdb.UserSubscribe(_usersub) > 0)
                            {
                                lblUnSubscribeMessage.Text = "Your email unsubscribed from our list!";
                                lblMessage.Text = "";
                            }
                            else
                            {
                                lblUnSubscribeMessage.Text = "";
                                lblMessage.Text = "Unsubscribed failed!Try again later!";
                            }
                        }
                        else
                        {
                            lblUnSubscribeMessage.Text = "";
                            lblMessage.Text = "Your email is already unsubscribed in our list!";
                        }
                    }
                    else
                    {
                        //Sakthi on 01 Dec 2011 -- Start
                        Int64 _usersubresult = _usersubdb.UserSubscribeOtherEmail(_usersub); 
                        if (_usersubresult > 0)
                        {
                            lblUnSubscribeMessage.Text = "Your email unsubscribed from our list!";
                            lblMessage.Text = "";
                        }
                        else if (_usersubresult == 0)
                        {
                            lblUnSubscribeMessage.Text = "";
                            lblMessage.Text = "Your email is already unsubscribed in our list!";
                        }
                        else
                        {
                            lblUnSubscribeMessage.Text = "";
                            lblMessage.Text = "Unsubscribed failed!Try again later!";
                        }
                        //lblUnSubscribeMessage.Text = "";
                        //lblMessage.Text = "No such email found.!";
                        //Sakthi on 01 Dec 2011 -- End
                    }  // Sakthi on 12 Jun 2012 -- End */
                }
                else
                {
                    lblUnSubscribeMessage.Text = "";
                    lblMessage.Text = "Wrong security check!";
                    divUNSError.Visible = true;
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                lblUnSubscribeMessage.Text = "";
                lblMessage.Text = "Security Check code was expired. Please try again.";
                divUNSError.Visible = true;
                CreateUpdateImageAndSession();
            }
        }
    }

    private void ClearUnSubscribe(bool OnOff)
    {
        divUnSubscribe.Visible = OnOff;
        txtUnSubscribeEmail.Text = "";
        txtSecurityCode.Text = "";
        lblUnSubscribeMessage.Text = "";
        lblMessage.Text = "";
        divUNSError.Visible = false;
    }

    protected void btnSendFeedBack_Click(object sender, EventArgs e)
    {
        Page.Validate("feedback");
        ChangeValidationEffectServerSide("feedback");
        if (Page.IsValid)
        {
            Int64 _result = 0;
            string strMessage = "";
            strMessage += "<br/>Sender : " + Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtName.Text)) + "<br /><br />";
            strMessage += "Email : " + txtFBEmail.Text + "<br /><br />";
            //strMessage += "For : " + ddlFormName.Text + "<br /><br />";
            //strMessage += "Type : " + ddlFeedBackType.Text + "<br /><br />";
            strMessage += "What you feel?  : " + GetFeedback() + "<br /><br />";
            strMessage += "Feedback Type  : " + GetEmotion() + "<br /><br />";
            strMessage += "Message : <br/>" + Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtFeedBack.Text)) + "<br />";

            UserFeedback _userfeedback = new UserFeedback();
            UserFeedbackDB _userfeedbackdb = new UserFeedbackDB();

            _userfeedback.FeedbackId = 0;
            _userfeedback.RefCEUserId = 0;
            if (Session[General.UserId] != null)
            {
                _userfeedback.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            }
            _userfeedback.EmotionType = Convert.ToInt16(txtEmotionType.Text);
            _userfeedback.FeedbackType = GetFeedbackType();
            _userfeedback.Name = Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtName.Text));
            _userfeedback.Email = txtFBEmail.Text.Trim();
            _userfeedback.Feedback = Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtFeedBack.Text));
            _userfeedback.Status = true;
            _result = _userfeedbackdb.Insert(_userfeedback);

            if (CE.Mail.MyMailManager.SendFeedBack(txtFBEmail.Text, CE.Configuration.MyConfigurationManager.GetAppSettingValue("FeedBackReceiver"), "CompareXpress Feedback", strMessage))
            {
                //Response.Redirect("UserMessage.aspx?ErrorCode=911");
                Response.Redirect("contactus.aspx?sc=1");
            }
            else
            {

            }
        }
    }

    private Int16 GetFeedbackType()
    {
        Int16 _result = 0;
        if (txtFeedbackType.Text == "0")
        {
            _result = 1;
        }
        else
        {
            _result = Convert.ToInt16(txtFeedbackType.Text);
        }
        return _result;
    }

    private string GetFeedback()
    {
        string _result = "";
        if (txtFeedbackType.Text == "1")
        {
            _result = "Suggestion";
        }
        else if (txtFeedbackType.Text == "2")
        {
            _result = "Compliment";
        }
        else if (txtFeedbackType.Text == "3")
        {
            _result = "Bug Report";
        }
        return _result;
    }

    private string GetEmotion()
    {
        string _result = "";
        if (txtEmotionType.Text == "1")
        {
            _result = "Very Useful";
        }
        else if (txtEmotionType.Text == "2")
        {
            _result = "Good";
        }
        else if (txtEmotionType.Text == "3")
        {
            _result = "OK";
        }
        else if (txtEmotionType.Text == "4")
        {
            _result = "Sad";
        }
        else if (txtEmotionType.Text == "5")
        {
            _result = "Angry";
        }
        return _result;
    }

}
